// pages/tixian/tixian.js
var app = getApp();
var utils = require('../../utils/shop/formatShop.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    balance: 0, //可提现佣金
    initInfo: {},
    index: 0,
    banklist:[],

    tx_money: ''
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideLoading();
  },

  //页面加载
  load: function () {
    //获取会员银行卡
    var bankArgs = {
      act: 'get_user_withdraw_method',
      User_ID: app.globalData.userInfo.User_ID
    };
    app.http_req(bankArgs, app.globalData.init.api_url, 'POST', this.getBankcard_callback);
  },

  //获取用户已有的银行卡回调
  getBankcard_callback: function (res) {
    if (res.errorCode == 0) {
      /**
       * 处理提现卡号，卡号中间部分用*代替
       */
      for (var i in res.data) {
        res.data[i].Method_Name = res.data[i].Method_Name + ' ' + utils.replace_str(res.data[i].Account_Val);
      }
      this.setData({
        banklist: res.data,
        balance: res.balance
      })
    } else if (res.errorCode == 2) {
      wx.navigateTo({
        url: '/pages/bankadd/bankadd'
      })
    } else {
      wx.showModal({
        title: '提示',
        content: typeof res.msg != 'undefined' ? res.msg : '获取信息失败',
        showCancel: false,
        success: function (res) {
          wx.navigateBack({
            delta: 1
          });
        }
      })
    }
  },

  //输入订单金额
  inputMoney: function (e) {
    var money = e.detail.value.length == 0 ? '' : e.detail.value;
    if (!utils.check_money_in(money)) {
      money = money.length > 0 ? money.slice(0, -1) : '';
    }
    
    this.setData({
      tx_money: money
    })
  },

  //申请提现提交
  formSubmit: function (form) {
    var form_info = form.detail.value;
    //判断是否选择提现方式
    if (!form_info.Money) {
      wx.showModal({
        title: '提示',
        content: '请填写提现金额',
        showCancel: false
      })
      return false;
    }
    /**
     * 判断金额是否正确
     */    
    if (!utils.check_money(form_info.Money)) {
      wx.showModal({
        title: '提示',
        content: '请正确填写提现金额',
        showCancel: false
      })
      return false;
    }
    //判断提现金额是否在可提现范围内
    var balance = this.data.balance;
    if (form_info.Money > balance || form_info.Money <= 0) {
      wx.showModal({
        title: '提示',
        content: '最多可提现' + balance + '元',
        showCancel: false
      })
      return false;
    }
    /**
     * 写入分销商提现记录
     */
    var dataArgs = {
      uri: '/api/' + app.globalData.userInfo.Users_ID + '/distribute/ajax/',
      action: 'withdraw_appy',
      User_ID: app.globalData.userInfo.User_ID,
      User_Method_ID: this.data.banklist[parseInt(form_info.Method_Name)].User_Method_ID,
      money: form_info.Money
    };    
    app.http_req(dataArgs, app.globalData.init.api_url, 'POST', this.withdraw_submit_callback);    
  },

  //写入分销商提现记录回调
  withdraw_submit_callback: function (res) {
    if (res.status === 1) {
      this.setData({
        balance: this.data.balance - this.data.tx_money,
        tx_money: ''
      });
      wx.showModal({
        title: '提示',
        content: typeof res.msg != 'undefined' ? res.msg : '提现提交成功，请等待通过审核',
        showCancel: false,
        success: function () {
          wx.navigateTo({
            url: '/pages/tixianlist/tixianlist'
          })
        }
      })
    } else {
      wx.showModal({
        title: '错误',
        content: res.msg,
        showCancel: false
      })
    }
  },

  /**
    * 选择银行卡号
    */
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },

  /**
   * 跳转到提现明细中
   */
  txlist_url:function () {
    wx.navigateTo({
      url:'/pages/tixianlist/tixianlist'
    })
  },

  /**
   * 添加银行卡
   */
  addbank_url: function () {
    wx.navigateTo({
      url: '/pages/bankadd/bankadd'
    })
  },
  

  /**
   * 银行卡管理
   */
  banklist_url: function () {
    wx.navigateTo({
      url: '/pages/banklist/banklist'
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //用户登录检查
    if (!app.globalData.userInfo) {
      app.login('', this.load);
    } else {
      this.load();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

})